using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GMailNotifierNET.Classes.BrowserInformation;
using System.Configuration;
using GMailNotifierNET.Classes;

namespace GMailNotifierNET.Forms
{
    public partial class FSettings : Form
    {
        private BrowserFetcher m_browserFetcher;
        private readonly string UNIQUE_AUTORUN_NAME = Application.ProductName + "_" + Utility.UniqueApplicationID.ToString();

        public FSettings()
        {
            InitializeComponent();
        }

        private void FSettings_Load(object sender, EventArgs e)
        {
            #region Fill Browserlist
            m_browserFetcher = new BrowserFetcher();

            ImageList imgList = new ImageList();
            lstBrowsers.SmallImageList = imgList;

            int i = 0;

            foreach (BrowserItem browser in m_browserFetcher.BrowserItems)
            {
                imgList.Images.Add(browser.IconDetails.Icon);

                ListViewItem lvItem = null;
                
                lvItem = new ListViewItem(browser.BrowserName);
                lvItem.SubItems.Add(browser.Command);
                lvItem.ImageIndex = i;
                lvItem.Tag = browser;

                lstBrowsers.Items.Add(lvItem);

                i++;
            }

            lstBrowsers.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            #endregion 
        }

        private void FSettings_Shown(object sender, EventArgs e)
        {
            #region Load Tab "Common"
            txtUsername.Text = Settings.Default.GMailUsername;
            txtPassword.Text = Settings.Default.GMailPassword;
            chkDontSavePassword.Checked = Settings.Default.DontSavePassword;

            numNewMailCheckInterval.Value = Settings.Default.NewMailCheckIntervalSeconds;
            numPreviewVisibleInterval.Value = Settings.Default.PreviewVisibleInterval;

            chkAutorun.Checked = RegistryAutorun.AutorunExists(UNIQUE_AUTORUN_NAME);
            #endregion

            #region Load Tab "Browser"
            foreach (ListViewItem lvItem in lstBrowsers.Items)
            {
                if (Settings.Default.CurrentBrowser == ((BrowserItem)lvItem.Tag).BrowserKey)
                {
                    lvItem.Selected = true;
                    lvItem.Focused = true;
                    lvItem.EnsureVisible();
                    lstBrowsers.Select();

                    break;
                }
            }
            #endregion
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            #region Save Tab "Common"
            Settings.Default.DontSavePassword = chkDontSavePassword.Checked;
            Settings.Default.GMailUsername = txtUsername.Text;

            if (chkDontSavePassword.Checked)
                Settings.Default.GMailPassword = "";
            else
                Settings.Default.GMailPassword = txtPassword.Text;

            if (chkAutorun.Checked)
                RegistryAutorun.SetAutorun(UNIQUE_AUTORUN_NAME, Application.ExecutablePath);
            else
                RegistryAutorun.RemoveAutorun(UNIQUE_AUTORUN_NAME);

            Settings.Default.NewMailCheckIntervalSeconds = Convert.ToUInt32(numNewMailCheckInterval.Value);
            Settings.Default.PreviewVisibleInterval = Convert.ToUInt32(numPreviewVisibleInterval.Value);
            #endregion

            #region Save Tab "Browser"
            if (lstBrowsers.SelectedItems.Count > 0)
                Settings.Default.CurrentBrowser = ((BrowserItem)lstBrowsers.SelectedItems[0].Tag).BrowserKey;
            #endregion

            Settings.Default.Save();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSetDefaultBrowser_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvItem in lstBrowsers.Items)
            {
                if ((BrowserItem)lvItem.Tag == m_browserFetcher.DefaultBrowser)
                {
                    lvItem.Selected = true;
                    lvItem.Focused = true;
                    lvItem.EnsureVisible();
                    lstBrowsers.Select();
                    break;
                }
            }
        }
    }
}